﻿using ETA.ClientService;
using ETA.Dtos;
using ETA.Entity;
using ETA.IService;
using ETA.Web.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    [AuthFilter("财务管理员")]
    public class FinanceAdminController : BaseController
    {
        private static IReferrerLogService _IReferrerLogService { set; get; }
        private static IUserService _IUserService { set; get; }

        public FinanceAdminController(IReferrerLogService IReferrerLogService, IUserService IUserService)
        {
            _IReferrerLogService = IReferrerLogService;
            _IUserService = IUserService;
        }


        /// <summary>
        /// 积分兑换
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public ActionResult HandlerPoint(string UserId, string logStr)
        {
            var res = new Response();
            var logArr = logStr.Split('$');
            if (logArr.Length > 0)
            {
                var user = UserBll.GetCompltetUserInfo(UserId);
                ViewBag.User = user;

                var list = new List<ReferrerLog>();
                var count = 0;
                var response = _IReferrerLogService.QueryReferrerLogList(
                new QueryReferrerLogRequest
                {
                    UserId = UserId,
                    Page = 0,
                    PageSize = int.MaxValue
                }, out count
                );
                if (response.Result.IsSuccess)
                {
                    list = response.ReferrerLogs.ToList();
                    foreach (var item in logArr)
                    {
                        if (string.IsNullOrEmpty(item))
                            continue;

                        var singleItemArr = item.Split('@');
                        if (singleItemArr.Length != 2)
                        {
                            res.Result.ErrMessage = "参数格式有误";
                            return JsonWithDate(res);
                        }
                        else
                        {
                            var logId = singleItemArr[0];
                            var changPoint = Int32.Parse(singleItemArr[1]);
                            var log = list.FirstOrDefault(l=>l.ReferrerLogId==logId);

                            var registrationBind = log.Referrer.RegistrationBind;
                            if (registrationBind != null && registrationBind.IsDelete == false)
                            {
                                var usedPoint = 0;
                                if (log.UsedPoint.HasValue)
                                {
                                    usedPoint = log.UsedPoint.Value;
                                }
                                if ((log.Point - usedPoint - changPoint) >= 0)
                                {
                                    log.UsedPoint = usedPoint + changPoint;
                                    var r = HandlerSingle(log);
                                    if (!r.Result.IsSuccess)
                                    {
                                        return JsonWithDate(r);
                                    }
                                    else
                                    {
                                        res.Result.IsSuccess = true;
                                    }
                                }
                                else
                                {
                                    res.Result.IsSuccess = false;
                                    res.Result.ErrMessage = "可兑换积分不够";
                                    //可兑换积分不够
                                }
                            }
                            else
                            {
                                //未出国无法兑换
                                res.Result.IsSuccess = false;
                                res.Result.ErrMessage = "未出国无法兑换";
                            }
                        }
                    }
                }
            }
            else
            {
                res.Result.ErrMessage = "没有填写任何兑换分值";
            }
            return JsonWithDate(res);
        }
        private Response HandlerSingle(ReferrerLog log)
        {
            var modifyLogResponse = _IReferrerLogService.ModifyReferrerLog(new ModifyReferrerLogRequest
            {
                ReferrerLog = log
            });
            return modifyLogResponse;
        }
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetReferrerLogSumList(QuerySumReferrerLogRequest req)
        {
            var list = new List<ReferrerLogSumDto>();
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogSumList(req, out count);
            if (response.Result.IsSuccess)
            {
                list = response.ReferrerLogSumDtos.ToList();
            }
            return JsonWithDate(list);
        }



        public ActionResult GetReferrerLogSumListCount(QuerySumReferrerLogRequest req)
        {
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogSumList(req, out count);
            return JsonWithDate(count);
        }


        public ActionResult ReferrerLogDetail(string UserId)
        {
            if (!string.IsNullOrEmpty(UserId))
            {
                var user = UserBll.GetCompltetUserInfo(UserId);
                ViewBag.User = user;

                var list = new List<ReferrerLog>();
                var count = 0;
                var response = _IReferrerLogService.QueryReferrerLogList(
                new QueryReferrerLogRequest
                {
                    UserId = UserId,
                    Page = 0,
                    PageSize = int.MaxValue
                }, out count
                );
                if (response.Result.IsSuccess)
                {
                    list = response.ReferrerLogs.ToList();
                }
                return View(list);
            }
            else
            {
                return Content("用户id丢失");
            }
        }

        public ActionResult GetReferrerLogList(QueryReferrerLogRequest filter)
        {
            var list = new List<ReferrerLog>();
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogList(
            filter, out count
            );
            if (response.Result.IsSuccess)
            {
                list = response.ReferrerLogs.ToList();
            }
            return JsonWithDate(list);
        }



        public ActionResult GetReferrerLogCount()
        {
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogList(
           new QueryReferrerLogRequest
           {
               UserId = CurrentUser.UserId
           }, out count
           );
            return JsonWithDate(count);
        }
    }
}
